<template>
  <div>
    <a-card :bordered="false">
      <f-search-form ref="searchForm" @searchHandler="searchHandler" :tableObj="$refs.table">
        <f-search-form-item label="服务WBS" name="serviceWbsCode"/>
        <f-search-form-item label="服务代码" name="serviceCode"/>
        <f-search-form-item label="项目（名称）" name="materiaSpecs"/>
        <f-search-form-item label="层级号" name="levelNo"/>
        <f-search-form-item label="科目名称" name="subjectName" slot="advanced"/>
        <f-search-form-item label="区域" name="materialArea" slot="advanced"/>
        <f-search-form-item label="系统" name="materialUse" slot="advanced"/>
        <f-search-form-item label="历史版本" name="versionId" type="select" ref="versionData200" :showAll="true"
                            :autoLoad="true" :data="api.bmp200.selectVersionList" labelKey="versionNo"
                            valueKey="versionId" slot="advanced" :params="detailsParams"/>
        <f-search-form-item label="履历筛选" name="planType" type="select" :data="hDataDemo" slot="advanced" :showAll="true"
                            @change="typeChange"/>
      </f-search-form>
      <f-table ref="table" rowKey="materialPackage100Id" :scroll="{x:columns.length*150,y:columns.length*35}"
               :showSetting="true"
               :tableColumns="columns"
               tableName="bmp200DetailsList"
               :exportBtn="false" :multiple="true" :operator="operator" :params="queryParam" :api="api.bmp200"
               handler="materialPackagePageListHandler">
        <span slot="applayStatus" slot-scope="text, record">
            <a-tag v-if="record.applayStatus ===''">--</a-tag>
            <a-tag v-if="record.applayStatus !=='' && record.applayStatus == 0">待审核</a-tag>
            <a-tag v-if="record.applayStatus == 1" color="pink">审核中</a-tag>
            <a-tag v-if="record.applayStatus == 2" color="green">审批通过</a-tag>
        </span>
       <span slot="action" slot-scope="text, record">
          <template v-if="record.costQuantity100==0&&record.parentId==0">
           <a @click="editSupplierNum(record)">编辑合同量</a>
          </template>
         <!--<template>-->
         <!--<a @click="$refs.drawer.showDrawer(record)">编辑</a>-->
         <!--<a-divider type="vertical"/>-->
         <!--</template>-->
         <!--<a-dropdown>-->
         <!--<a class="ant-dropdown-link">更多 <a-icon type="down"/></a>-->
         <!--<a-menu slot="overlay">-->
         <!--<a-menu-item v-if="$auth('table.delete')">-->
         <!--<a href="javascript:;" @click="$refs.table.del(record)">删除</a>-->
         <!--</a-menu-item>-->
         <!--</a-menu>-->
         <!--</a-dropdown>-->
        </span>
      </f-table>
      <upload-modal ref="uploadModal" :action="api.bmp200.import200UpdateData" @change="uploadEnd()" :otherParams="detailsParams"></upload-modal>
      <bmp-update-drawer ref="updateDrawer" :listType="listType" :detailsParams="detailsParams"></bmp-update-drawer>
      <bmp-supplier-num-drawer ref="bmpSupplierNumDrawer" :detailsParams="detailsParams"></bmp-supplier-num-drawer>
    </a-card>
  </div>
</template>
<script>
  import bmpUpdateDrawer from './bmp_update_drawer.vue'
  import bmpSupplierNumDrawer from './bmp_suppliernum_drawer.vue'
  import api from './js/api'

  export default {
    name: 'TableList',
    props: {
      detailsParams: {}
    },
    components: {
      bmpUpdateDrawer,
      bmpSupplierNumDrawer
    },
    data() {
      let me = this
      //操作按钮
      let operator = []

      operator.push({
        name: '修正变更',
        icon: '',
        that: this,
        handler: function(table, that) {
          that.http.get(that, {
            url: that.api.project.srmContractsCK,
            data: {
              projectId: that.queryParam.projectId
              ,packageNo: that.queryParam.packageNo
            },
            success: function(me, data) {
              let ck = data
              if (ck != 0) {
                me.$message.error(ck.message)
              } else {
                that.listType = 1
                that.$refs.updateDrawer.showDrawer(1)
              }
            }
          })
        }
      })
      operator.push({
        name: '追加变更',
        icon: '',
        that: this,
        handler: function(table, that) {
          that.http.get(that, {
            url: that.api.project.srmContractsCK,
            data: {
              projectId: that.queryParam.projectId
              ,packageNo: that.queryParam.packageNo
            },
            success: function(me, data) {
              let ck = data
              if (ck != 0) {
                me.$message.error(ck.message)
              } else {
                that.listType = 2
                that.$refs.updateDrawer.showDrawer(2)
              }
            }
          })

        }
      })
      operator.push({
        name: '导出',
        icon: '',
        that: this,
        handler: function(table, that) {
          let columns = table.getExportColumns(that.columns)
          that.detailsParams['columns'] = JSON.stringify(columns)
          that.download200Template(table)
        }
      })
      operator.push({
        name: '导入',
        icon: '',
        that: this,
        handler: function(table, that) {
          that.http.get(that, {
            url: that.api.project.projectStatus,
            data: {
              projectId: that.queryParam.projectId
            },
            success: function(me, data) {
              let projectStatus = data
              if (projectStatus == 1) {
                me.$message.error('项目已锁定不支持操作')
              } else {
                that.$refs.uploadModal.open()
              }
            }
          })
        }
      })
      operator.push({
        name: '分承包合同下达',
        icon: '',
        that: this,
        handler: function(table, that) {
          that.http.get(that, {
            url: that.api.project.projectStatus,
            data: {
              projectId: that.queryParam.projectId
            },
            success: function(me, data) {
              let projectStatus = data
              if (projectStatus == 1) {
                me.$message.error('项目已锁定不支持操作')
              } else {
                that.getSrmPms05Req()
              }
            }
          })
        }
      })
      return {
        refs: this.$refs,
        api: Object.assign(api, this.api),
        //列表初始化查询参数
        queryParam: {},
        vDataDemo: [{ label: '100.1', value: 1 }, { label: '100.2', value: 2 }, { label: '200.0', value: 3 }],
        hDataDemo: [{ label: '修正变更', value: 1 }, { label: '追加变更', value: 2 }],
        listType: 1, // 1 修正计划  追加变更
        // 表头
        columns: [
          {
            title: '序号',
            width: 80,
            align: 'center',
            dataIndex: '#',
            customRender: (text, record, index) => index + 1
          },
          {
            title: '分包Id',
            width: 180,
            align: 'center',
            dataIndex: 'materialPackage100Id'
          },
          {
            title: '项目代码',
            width: 150,
            align: 'center',
            dataIndex: 'projectCode'
          },
          {
            title: '项目名称',
            width: 150,
            align: 'center',
            dataIndex: 'projectName'
          },
          {
            title: '服务代码',
            width: 150,
            align: 'center',
            dataIndex: 'serviceCode'
          },
          {
            title: '服务WBS代码',
            width: 150,
            align: 'center',
            dataIndex: 'serviceWbsCode'
          },
          {
            title: '项目（名称）',
            width: 150,
            align: 'center',
            dataIndex: 'materiaSpecs'
          },
          {
            title: '科目名称',
            width: 150,
            align: 'center',
            dataIndex: 'subjectName'
          },
          {
            title: '层级号',
            width: 150,
            align: 'center',
            dataIndex: 'levelNo'
          },
          {
            title: '分承包发包项目描述',
            width: 200,
            align: 'center',
            dataIndex: 'packageDesc'
          },
          {
            title: '区域',
            width: 150,
            align: 'center',
            dataIndex: 'materialArea'
          },
          {
            title: '系统',
            width: 150,
            align: 'center',
            dataIndex: 'materialUse'
          },
          {
            title: '标前工程量',
            width: 150,
            align: 'center',
            dataIndex: 'beforeMarkQuantity'
          },
          {
            title: '200预算成本工程量',
            width: 200,
            align: 'center',
            dataIndex: 'costQuantity200'
          }
        ],
        operator: operator
      }
    },
    created() {
      let params = { ...this.detailsParams }
      params.isGroup = false
      this.queryParam = params
      this.getBusProjectSupplierList()
      //this.refreshData()
    },
    methods: {
      getBusProjectSupplierList() {
        this.http.get(this, {
          url: this.api.bmp200.getBusProjectSupplierList,
          data: this.detailsParams,
          success(me, data) {
            console.log(data)
            for (let i in data) {
              let info = data[i]
              me.columns.push({
                title: info.supplierName + '合同量',
                width: 250,
                align: 'center',
                dataIndex: 'supplierNum' + info.index
              })
            }
            me.columns.push({
                title: 'oa审批状态',
                width: 150,
                align: 'center',
                dataIndex: 'applayStatus',
                scopedSlots: { customRender: 'applayStatus' }
            })
            me.columns.push({
              title: '操作',
              width: 150,
              align: 'center',
              dataIndex: 'action',
              scopedSlots: { customRender: 'action' },
              fixed: 'right'
            })
          }
        })
      },
      typeChange(val) {
        this.detailsParams.planType = val
        this.refs.versionData200.initData(this.detailsParams)
        this.refs.searchForm.resetFormFields('versionId')
      },
      download200Template(table) {
        let that = this
        let queryParams = { ...table.searchParams }
        let detailsParams = { ...that.detailsParams }
        let params = Object.assign(detailsParams, queryParams)
        that.http.exportExcel(that, {
          url: that.api.bmp200.export200Template,
          data: params
        })
      },
      getSrmPms05Req() {
        let that = this
        that.http.post(that, {
          url: that.api.bmp200.getSrmPms05Req,
          data: that.detailsParams,
          success(me, data) {
            me.$message.success('下达成功')
          }
        })
      },
      uploadEnd() {
        this.$refs.uploadModal.close()
        this.$message.success('导入成功')
        this.$refs.table.refresh(false, this.queryParam)
      },
      editSupplierNum(record) {
        this.http.get(this, {
          url: this.api.project.projectStatus,
          data: {
            projectId: record.projectId
          },
          success: function(me, data) {
            let projectStatus = data
            if (projectStatus == 1) {
              me.$message.error('项目已锁定不支持操作')
            } else {
              $refs.bmpSupplierNumDrawer.showDrawer(record)
            }
          }
        })
      },
      refreshData() {
        this.$bus.$on('refreshDetailsMaterialPackage200', () => {
          this.$refs.table.refresh(false, this.queryParam)
        })
      }
    }
  }
</script>
